/**
 * 
 */
package pl.polsl.pum2.pj.math;

import javax.microedition.khronos.opengles.GL10;

/**
 * Class managing camera rotations. <br/>
 * Camera always looks at (0, 0, 0) and circles around this point within <code>radius</code> in 3D.
 */
public class QCamera {

  private Quaternion3d rot;
  private float        radius;
  private float        sensivity;

  /**
   * @param radius
   *          distance between Look-at-point and the camera.
   */
  public QCamera(float radius, float sensivity) {
    this.radius = radius;
    this.sensivity = sensivity;
    rot = Quaternion3d.identity();
    rotate(1f, 1f, 1571L); // apply an initial rotation
  }

  public void rotate(float dx, float dy, long dt) {
    Quaternion3d dq = Quaternion3d.fromEulerAngles(-dy * sensivity * dt, -dx * sensivity * dt, 0);
    rot = Quaternion3d.multiply(rot, dq);
  }

  public void setView(GL10 gl) {
    float[] matrix = new float[16];
    Quaternion3d.setMatrixFromQuaternion(matrix, rot);
    gl.glTranslatef(0, 0, -radius);
    gl.glMultMatrixf(matrix, 0);
  }
}
